﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

namespace Barkod.Utility
{
    public class RepositoryManager
    {
        public static RepositoryItemGridLookUpEdit GetGLESablon(DataTable SourceTable, string DisplayMember, string ValueMember)
        {
            RepositoryItemGridLookUpEdit gleSablon = new RepositoryItemGridLookUpEdit();
            gleSablon.PopupFilterMode = DevExpress.XtraEditors.PopupFilterMode.Contains;
            //search columns via the View's OptionsFind.FindFilterColumns property. In the "StartsWith" or "Contains" mode, the search is performed against a field specified by the RepositoryItemSearchLookUpEdit.DisplayMember property.

            gleSablon.AllowNullInput = DefaultBoolean.True;
            gleSablon.View.OptionsView.ColumnAutoWidth = false;
            gleSablon.View.OptionsView.EnableAppearanceEvenRow = true;
            gleSablon.View.OptionsView.ShowAutoFilterRow = true;
            gleSablon.View.OptionsBehavior.Editable = false;
            gleSablon.View.BestFitMaxRowCount = 50;
            gleSablon.View.OptionsFind.HighlightFindResults = true;
            gleSablon.View.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            gleSablon.TextEditStyle = TextEditStyles.Standard; // DisableTextEditor;
            gleSablon.NullText = string.Empty;
            gleSablon.ImmediatePopup = true;

            gleSablon.PopupFormMinSize = new Size(500, 500);
            gleSablon.BestFitMode = BestFitMode.BestFit;

            gleSablon.Popup += gleSablon_Popup;

            //Form2.LookUpEdit.Properties.BestFitMode = BestFitMode.BestFitResizePopup;
            //Form2.LookUpEdit.Properties.SearchMode = SearchMode.AutoComplete;
            //Form2.LookUpEdit.Properties.AutoSearchColumnIndex = 1;
            //ri.View.OptionsFind.AllowFindPanel = true;
            //ri.View.OptionsView.AllowCellMerge = true;
            //riMaster.AutoComplete = false;            
            //riMaster.ShowDropDown = ShowDropDown.DoubleClick;
            //I have a ComboBoxEdit with AutoComplete=True and TextEditStyle=DisableTextEditor. When the user starts typing and a matching item is found the selection changes to that item, with the series of matching characters hightlighted. Is there a way to get that hightlighted text? 
            //The same suggestion is applied to the ProgressBarControl: it must always use the DisplayFormat and CustomDisplayText regardless of its focused state, because it's a "read-only editor".

            gleSablon.DataSource = SourceTable;
            gleSablon.DisplayMember = DisplayMember;
            gleSablon.ValueMember = ValueMember;

            gleSablon.PopulateViewColumns();

            if (SourceTable.TableName == "V_Kumas")
            {
                gleSablon.View.Columns["FinishId"].Caption = "Finish";
                gleSablon.View.Columns["KaliteAciklama"].Caption = "DiFinish";
            }

            foreach (GridColumn gc in gleSablon.View.Columns)
            {
                gc.FilterMode = ColumnFilterMode.DisplayText;
                 
            }

            return gleSablon;
        }

        static void gleSablon_Popup(object sender, EventArgs e)
        {
            GridLookUpEdit gle = (GridLookUpEdit)sender;
            gle.Properties.View.BestFitColumns();
            gle.Popup -= gleSablon_Popup;
        }

    }
}
